import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { baiscRouters, asyncRouters } from '@/router/routes/index'
import { createPermissionGuard } from '@/router/guard/permission-guard'

const { VITE_HASH_ROUTE = fase, VITE_BASE_URL } = import.meta.env

/**
 * 创建路由实例
 */
export const router = createRouter({
  history: VITE_HASH_ROUTE === true ? createWebHashHistory(VITE_BASE_URL) : createWebHistory(VITE_BASE_URL),
  // 处理固定路由
  routes: [],
  // 设置新页面起始位置 -》 始终滚动到顶部
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

/**
 * 安装路由
 */
export async function setupRouter(app) {
  baiscRouters.forEach((route) => router.addRoute(route))
  app.use(router)
  createPermissionGuard(router)
  // 判断路由是否初始化成功
  await router.isReady()
}
